//
//  VCForDestination_contents.m
//  SeeTheWorld
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "VCForDestination_contents.h"
#import "CellForDestination_sontents.h"
#import "ModelForDestination_contents.h"
#import "PCH.pch"
#import <UIImageView+WebCache.h>


@interface VCForDestination_contents () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, copy) NSString *stringForUrl;
// 装model 的数组.
@property (nonatomic, retain) NSMutableArray *arrayForTravelPlace;
// tableView.
@property (nonatomic, retain) UITableView *tableViewForTravelPlace;
// cell 重用池名.
@property (nonatomic, copy) NSString *stringForTravelPlace;

@property (nonatomic, retain)UIButton *buttonOfLeft;

@end

@implementation VCForDestination_contents

- (void)dealloc {
    self.tableViewForTravelPlace.dataSource = nil;
    self.tableViewForTravelPlace.delegate = nil;
    [_arrayForTravelPlace release];
    [_tableViewForTravelPlace release];
    [_stringForTravelPlace release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    /** 主页navigation左侧button */
    self.buttonOfLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOfLeft.frame = CGRectMake(5, 10, 30, 30);
    [self.buttonOfLeft addTarget:self action:@selector(fanHui:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonOfLeft setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-2"] forState:UIControlStateNormal];
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithCustomView:self.buttonOfLeft];
    self.navigationItem.leftBarButtonItem = bar;
    [bar release];
    
    self.arrayForTravelPlace = [NSMutableArray array];
    
    self.stringForUrl = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/attractions/%@.json?per_page=20&page=1", self.id];
    
    self.navigationItem.title = [NSString stringWithFormat:@"%@", self.name_zh_cn];
    
    [self handleData];
    
    [self createTableView];
    
}

/** 视图将要出现. */
- (void)viewWillAppear:(BOOL)animated {
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    
    [self.tableViewForTravelPlace reloadData];
    
}
#pragma mark ** 点击返回按键执行下面代码
- (void)fanhui:(UIButton *)button {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark ** 处理数据
- (void)handleData {
    
    [NetworkingHandler networkingWithURL:self.stringForUrl completeHandle:^(NSData *data, id result) {
        
    
        for (NSDictionary *dic in result) {
            
            ModelForDestination_contents *model = [[ModelForDestination_contents alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.arrayForTravelPlace addObject:model];
            
            [model release];
            
        }
        
        [self.tableViewForTravelPlace reloadData];
        
    }];
    
}


#pragma mark ** 创建TableView
- (void)createTableView {
    
    self.tableViewForTravelPlace = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    
    self.tableViewForTravelPlace.dataSource = self;
    
    self.tableViewForTravelPlace.delegate = self;
    
    self.stringForTravelPlace = @"poolForTravelPlace";
    
    [self.tableViewForTravelPlace registerClass:[CellForDestination_sontents class] forCellReuseIdentifier:self.stringForTravelPlace];
    
    [self.view addSubview:self.tableViewForTravelPlace];
    
    [_tableViewForTravelPlace release];
    
    self.tableViewForTravelPlace.dk_backgroundColorPicker =  DKColorWithRGB(0xffffff, 0x343434);

    
}

#pragma mark ** UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return self.arrayForTravelPlace.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CellForDestination_sontents *cell = [tableView dequeueReusableCellWithIdentifier:self.stringForTravelPlace];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);

    [cell voluationToCellForForeignSubViewsWithModel:self.arrayForTravelPlace[indexPath.row]];
    
    return cell;
}


#pragma mark ** UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return HEIGHT / 7;
}


- (void)fanHui:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
